const app = getApp();

Page({
    data: {
        hasCreatedCard: '', // 初始值可以从全局数据获取
        cardInfo: {}, // 用于存储获取到的名片信息
        cardImagePaths: [], // 用于存储生成的名片图片路径
    },

    onLoad() {
        this.fetchCardInfo()
    },

    // 请求获取名片信息
    fetchCardInfo() {
        console.log(app.globalData.userInfo.id);
        console.log(app.globalData.cardImagePaths);

        wx.request({
            url: `http://localhost:8080/system/card/user/${app.globalData.userInfo.id}`, // 替换为你的后端接口地址
            method: 'GET',
            header: {
                'Content-Type': 'application/json',
                'Authorization': `Bearer ${wx.getStorageSync('token')}` // 如果需要带 Token
            },
            success: (res) => {
                if (res.statusCode === 200 && res.data.code === 200) {
                    // 成功获取名片信息
                    console.log('名片信息:', res.data.data.avatar);
                    console.log('After setting hasCreatedCard:', app.globalData.hasCreatedCard); // 打印修改后的值
                    if (app.globalData.cardImagePaths.length === 0) {
                        app.globalData.cardImagePaths.push(res.data.data.avatar);
                    }
                    console.log(app.globalData.cardImagePaths);

                    if (res.data.data.avatar) {
                        app.globalData.hasCreatedCard = true
                    }
                    console.log('After setting hasCreatedCard:', app.globalData.hasCreatedCard); // 打印修改后的值
                    this.setData({
                        hasCreatedCard: true,
                        cardImagePaths: app.globalData.cardImagePaths || []
                    });
                    console.log(app.globalData.hasCreatedCard);
                    console.log(app.globalData.cardImagePaths);
                    // const basePath = '/images/card/';
                    // const cardImagePaths = (app.globalData.cardImagePaths || []).map(path => basePath + path);
                    // this.setData({
                    //     cardImagePaths
                    // });
                    console.log(this.data.cardImagePaths);
                } else {
                    // 接口返回错误信息
                    console.error('获取名片信息失败:', res.data.msg);
                    wx.showToast({
                        title: '获取名片信息失败',
                        icon: 'none',
                    });
                }
            },
            fail: (err) => {
                // 请求失败
                console.error('请求失败:', err);
                wx.showToast({
                    title: '请求失败，请稍后再试',
                    icon: 'none',
                });
            },
        });
    },

    sendCard: function () {
        // 发送数字名片的逻辑
        console.log('Send card');
    },

    onShareAppMessage(e) {
        console.log('Share event triggered:', e);

        // 默认分享配置
        let shareOpt = {
            title: '欢迎体验本小程序', // 默认标题
            path: '/pages/index/index', // 默认分享路径
        };

        // 根据触发分享的元素调整分享内容
        if (e.from === 'button') { // 如果是从按钮触发的分享
            const targetId = e.target.id;
            if (targetId === "card") {
                shareOpt = {
                    title: "我的名片",
                    path: `/pages/index/index?openid=${this.data.userInfo.id}`, // 使用 data 中的 userInfo.id
                };
            } else if (targetId === "resume") {
                shareOpt = {
                    title: "我的简历",
                    path: `/pages/index/index?openid=${this.data.userInfo.id}`, // 使用 data 中的 userInfo.id
                };
            }
        }

        return shareOpt;
    },

    startSearch() {
        // 用户点击搜索按钮时触发
        const inputValue = this.data.inputValue;
        console.log('开始搜索:', inputValue);
        // 这里可以添加搜索逻辑，比如调用接口进行搜索
        wx.navigateTo({
            url: '/pages/searchCard/index',
        })
    },

    onSearchBlur() {
        // 当搜索框失去焦点时触发
        this.setData({
            isFocused: false
        });
    },

    switchToBusinessCard() {
        wx.navigateTo({
            url: '/pages/select/index'
        });
    },
    switchToResume() {
        wx.navigateTo({
            url: '/pages/resume/main'
        });
    },
    editCard: function () {
        // 编辑名片的逻辑
        console.log('Edit card');
        wx.navigateTo({
            url: '/pages/userInfo/index',
        })
    },

    changeStyle: function () {
        // 切换样式的逻辑
        console.log('Change style');
    },

    changeLanguage: function () {
        // 切换语言的逻辑
        console.log('Change language');
    },

});